#!/usr/bin/python2
# -*- coding: utf-8; mode: Python; indent-tabs-mode: t; -*-

import os
import os.path
import sys
import subprocess
import xml.etree.ElementTree as et

def get_build_tools_dir():
	android_home=os.environ["ANDROID_HOME"]
	base_build_tools_dir=os.path.join(android_home,"build-tools")
	for name in reversed(sorted(os.listdir(base_build_tools_dir))):
		path=os.path.join(base_build_tools_dir,name)
		if os.path.isdir(path):
			return path
	raise RuntimeError("No build tools directory")

def get_version_string(apk):
	project_path=os.path.dirname(os.path.dirname(apk))
	manifest_path=os.path.join(project_path,"AndroidManifest.xml")
	manifest=et.parse(manifest_path)
	return manifest.getroot().get("{http://schemas.android.com/apk/res/android}versionName")

if __name__=="__main__":
 	unsigned_apk=sys.argv[1]
	key_store=sys.argv[2]
	key_alias=sys.argv[3]
	version_string=get_version_string(unsigned_apk)
	suffix="-release-unsigned.apk"
	if unsigned_apk.endswith(suffix):
		base=unsigned_apk[:-len(suffix)]
	else:
		base=unsigned_apk[:-4]
	signed_apk=base+"-signed.apk"
	final_apk="{}-v{}.apk".format(base,version_string)
	build_tools_dir=get_build_tools_dir()
	aapt=os.path.join(build_tools_dir,"aapt")
	for path in subprocess.check_output([aapt,"list",unsigned_apk]).strip().split("\n"):
		if path.startswith("META-INF/"):
			print("Removing {}".format(path))
			subprocess.check_call([aapt,"remove",unsigned_apk,path])
	sign_cmd=["jarsigner","-verbose","-sigalg","SHA1withRSA","-digestalg","SHA1","-keystore",key_store,"-signedjar",signed_apk,unsigned_apk,key_alias]
	subprocess.check_call(sign_cmd)
	zipalign=os.path.join(build_tools_dir,"zipalign")
	align_cmd=[zipalign,"-f","-v","4",signed_apk,final_apk]
	subprocess.check_call(align_cmd)
